﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Input;
using Microsoft.DirectX.DirectInput;

namespace Microsoft.Samples.Kinect.WpfViewers {

    class InsertText {//: BaseCommand {
        public InsertText() {
            //this.SCommandName = "InsertText";
            //this.SMenuItemName = "Insert Te&xt";
            //this.SArgumentHelp = "Enter the text to be inserted:";
        }

        public const ushort KEYEVENTF_KEYUP = 0x0002;
        public const ushort KEYEVENTF_KEYDOWN = 0x0000;
        public const ushort KEYEVENTF_SCANCODE = 0x0008;
        public const ushort KEYEVENTF_UNICODE = 0x0004;

        public struct KEYBDINPUT {
            public ushort wVk;
            public ushort wScan;
            public uint dwFlags;
            public long time;
            public uint dwExtraInfo;
        };

        [StructLayout(LayoutKind.Explicit, Size = 28)]
        public struct INPUT {
            [FieldOffset(0)]
            public uint type;
            [FieldOffset(4)]
            public KEYBDINPUT ki;
        };


        [DllImport("user32.dll")]
        public static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);

        public void executeCommand() {
            INPUT structInput;
            structInput = new INPUT();
            structInput.type = 1;
            structInput.ki.wScan = 0;
            structInput.ki.wVk = 0;
            structInput.ki.time = 0;
            structInput.ki.dwFlags = 0;
            structInput.ki.dwExtraInfo = 0;
            //string SArgument = "A";

            Microsoft.DirectX.DirectInput.Key ScanCode = Microsoft.DirectX.DirectInput.Key.W;

            structInput.ki.dwFlags = KEYEVENTF_UNICODE;
            structInput.ki.wScan = (ushort)ScanCode;
            SendInput(1, ref structInput, Marshal.SizeOf(structInput));

            structInput.ki.dwFlags = KEYEVENTF_UNICODE;
            structInput.ki.dwFlags += KEYEVENTF_KEYUP;
            structInput.ki.wScan = (ushort)ScanCode;
            SendInput(1, ref structInput, Marshal.SizeOf(structInput));


        }
    }
}